<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改':'添加补差药品'"
    :visible="visible"
    width="960px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    @close="refresh">
    <el-form
      ref="form"
      :model="form"
      label-width="82px">
      <el-row :gutter="15" style="margin-bottom: 5px">
        <el-col :lg="6" :md="12">
      <el-form-item label="药品编码:" prop="invcode">
        <el-input
          clearable
          v-model="where.invcode"
          placeholder="输入药品编码"/>
      </el-form-item>
        </el-col>
          <el-col :lg="6" :md="12">
          <el-form-item label="生产厂家:" prop="bcname">
        <el-input
          clearable
          v-model="where.vmanufacturer"
          placeholder="输入生产厂家"/>
      </el-form-item>
          </el-col>
        <el-col :lg="6" :md="12">
          <div class="ele-form-actions">
            <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              size="small"
              @click="reload">查询
            </el-button>
            <el-button @click="reset" size="small">重置</el-button>
          </div>
        </el-col>

      </el-row>
    </el-form>
    <ele-pro-table
      ref="table"
      :where="where"
      :datasource="url"
      :columns="columns"
      :selection.sync="selection"
      :toolbar="false"
      height="calc(100vh - 345px)">
    </ele-pro-table>
    <div slot="footer">
      <el-button @click="updateVisible(false)">关闭</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">添加
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'SpbcInvSelect',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    pid: Number
  },
  data() {
    return {
      // 表格数据接口
      url: '/spbcinvset/selectinvcode',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          columnKey: 'index',
          type: 'index',
          label: '序号',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        // {
        //   prop: 'cmaterialvid',
        //   label: '药品主键',
        //   width: 80,
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   fixed: "left"
        // },
        {
          prop: 'zxspbm',
          label: '统编代码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'invcode',
          label: '药品编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'invname',
          label: '药品名称',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'gg',
          label: '规格',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'materialunit',
          label: '单位',
          showOverflowTooltip: true,
          minWidth: 50,
          align: 'left'
        },
        {
          prop: 'vmanufacturer',
          label: '生产厂家',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'cgy',
          label: '采购员',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'left'
        }
        // {
        //   prop: 'updateTime',
        //   label: '更新时间',
        //   sortable: 'custom',
        //   showOverflowTooltip: true,
        //   minWidth: 160,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     return this.$util.toDateString(cellValue);
        //   }
        // },
        // {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 150,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 表单数据
      //form: Object.assign({}, this.pid),
      form: {},
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.pid) {
        //this.form = Object.assign({}, this.data,this.pid);
        this.isUpdate = true;
      } else {
        //this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    reset() {
      this.where = {};
      this.reload();
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          for(let v of this.selection)
          {
            v.pid = this.pid;
          }
          this.$http.post('/spbcinvsetdetail/saveinvcode', this.selection).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              //this.updateVisible(false);
              //this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    refresh(){
      this.$emit('done');
    }
  }
}
</script>

<style scoped>
</style>
